package com.tank.jdk8;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created with IntelliJ IDEA.
 * User: SuQiEr
 * Date:2016/12/28
 * Time: 19:34
 * To change this template use File | Settings | File Templates.
 */
@Slf4j
public class StreamTest {


    // Count empty strings
    List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
    @Test
    public void test(){

        //统计空字符个数
       // Stream stream =strings.stream();
        //该类型 stream class:java.util.stream.ReferencePipeline.Head
      //  log.info("该类型 stream class:{}",stream.getClass().getCanonicalName());
        // stream.filter(string -> string.);
        Long count = strings.stream().filter(string -> string.isEmpty()).count();
        //log.info("空字符个数 count:{}",count);

        List<String> filtered  = strings.stream().filter(string ->!string.isEmpty()).collect(Collectors.toList());
        //System.out.println("Filtered List: " + filtered);

       String  mergedString = strings.stream().filter(string ->!string.isEmpty()).collect(Collectors.joining(", "));
        //System.out.println("Merged String: " + mergedString);

        //平方去重复
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        List<Integer> squaresList  = numbers.stream().map( i ->i*i).distinct().collect(Collectors.toList());
      //  System.out.println("Squares List: " + squaresList);

        List<Integer> integers = Arrays.asList(1,2,13,4,15,6,17,8,19);
        IntSummaryStatistics stats = integers.stream().mapToInt((x) ->x).summaryStatistics();

        System.out.println("Highest number in List : " + stats.getMax());
        System.out.println("Lowest number in List : " + stats.getMin());
        System.out.println("Sum of all numbers : " + stats.getSum());
        System.out.println("Average of all numbers : " + stats.getAverage());
        System.out.println("Random Numbers: ");

        //print ten random numbers
        Random random = new Random();

        log.info("for 循环");
        for(int i=0; i < 10; i++){
            System.out.println(random.nextInt());
        }

        log.info(" limit ");
        random.ints().limit(10).sorted().forEach(System.out::println);
        //parallel processing
        count = strings.parallelStream().filter(string -> string.isEmpty()).count();
        System.out.println("Empty Strings: " + count);
    }

    @Test
    public void testPerson() {

        List<Person> persons = new ArrayList<>();
        persons.add(new Person("tk", new BigDecimal("20.00")));
        persons.add(new Person("tmz", new BigDecimal("21.00")));
        final  BigDecimal amount = BigDecimal.ZERO;

    }
}

